package com.hubu.web;

import com.hubu.pojo.*;
import com.hubu.mapper.administratorUserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@WebServlet("/adminHome")
public class AdministratorHomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //request.setCharacterEncoding("UTF-8");
        try {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
                // 获取 Mapper
                administratorUserMapper userMapper = sqlSession.getMapper(administratorUserMapper.class);

                List<student_Information> students = userMapper.selectAllStudent();
                List<teacher_Information> teachers = userMapper.selectAllTeacher();

                // 将列表存储在请求属性中
                request.setAttribute("students", students);
                request.setAttribute("teachers", teachers);

                System.out.println(students);
                System.out.println(teachers);
                // 转发到 adminHome.jsp 页面
                RequestDispatcher dispatcher = request.getRequestDispatcher("adminHome.jsp");
                dispatcher.forward(request, response);
            }
        } catch (Exception e) {
            // 在此处理异常，记录或适当地响应
            e.printStackTrace();  // 仅作演示之用，应替换为适当的日志记录或响应处理
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }



}